//
//  XRTextInputView.m
//  XRmenu-HD
//
//  Created by Chow Akins on 11-12-23.
//  Copyright 2011 home user. All rights reserved.
//

#import "XRTextInputView.h"
#import "XRmenuCoreData.h"
#import "PluSpecialSort.h"
#import "PluSpecialCode.h"

@implementation XRTextInputView
@synthesize celldata,arrayCook;

////=========================================================== 
//// - (id)init
////
////=========================================================== 
//- (id)init
//{
//	NSDictionary *vCfg = [APPSESSION.allconfig findAtPath:@"UIKit CSS/UIView CSS/plu remarks view"];
//	if (vCfg) {
//		self = [super initWithConfig:vCfg];
//		if (self) {
//			[self inittextView:vCfg WithContent:nil];
//		}
//	}else {
//		self = [super init];
//	}
//	
//	self.celldata = nil;
//    return self;
//}

- (id)initWithOrderData:(DishOrder *)cdata
{
	NSDictionary *vCfg = [APPSESSION.allconfig findAtPath:@"UIKit CSS/UIView CSS/plu remarks view"];
	if (vCfg) {
		self = [super initWithConfig:vCfg];
		if (self) {
            if (cdata != nil) {
                [self inittextView:[vCfg findAtPath:@"text property"] WithContent:cdata.ddRemark];
            }
            else{
                [self inittextView:[vCfg findAtPath:@"text property"] WithContent:@""];
            }
		}
		self.celldata = cdata;
	}else {
		self = [super init];
		self.celldata = nil;
	}
    //初始化烧法字符串数组
    arrayCook=[[NSMutableArray alloc] init];
    if (cdata.rsToPluPlu.rsToPluSort.rsToPluSpecialSort != nil &&
        [cdata.rsToPluPlu.rsToPluSort.rsToPluSpecialSort count] > 0) {
        for (PluSpecialSort *specialSort in cdata.rsToPluPlu.rsToPluSort.rsToPluSpecialSort) {
            if (specialSort.rsToPluSpecialCode != nil &&
                 [specialSort.rsToPluSpecialCode count] > 0) {
                for (PluSpecialCode *specialCode in specialSort.rsToPluSpecialCode) {
                    [arrayCook addObject:JsonFiled(specialCode.psDescripts)];
                }
            }
        }
    }
    
    //画出tableview
    NSDictionary *tdict = [APPSESSION.allconfig findAtPath:@"UIKit CSS/UIView CSS/plu remarks view"];
    NSDictionary *tabdict = [[[APPSESSION.allconfig objectForKey:@"UIKit CSS"] objectForKey:@"TableView CSS"] objectForKey:[tdict getStringValueAtPath:@"sub tableview/table view style"]];
    if (tdict) {
        [self setTableViewProperty:tabdict Frame:CGRectFromString([tdict getStringValueAtPath:@"sub tableview/table view rect"])];
    }
    
    UIColor *btn_color = RGBA(235.0, 217.0, 185.0, 254.0/ 255.0f);
    NSString *btn_bkg=[XRmenuPublic getGeneralSkinImg:@"MainView/MyOrderView/bt_myorder_main.png"];
    if ([self viewWithTag:kTagOfStartBT + 0]) {
        UIButton *btn =(UIButton *) [self viewWithTag:kTagOfStartBT + 0];
        [btn.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
        [btn setTitle:loc(@"保存") forState:UIControlStateNormal];
        [btn setTitleColor:btn_color forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageWithContentsOfFile:btn_bkg] forState:UIControlStateNormal];
    }
    if ([self viewWithTag:kTagOfStartBT + 1]) {
        UIButton *btn =(UIButton *) [self viewWithTag:kTagOfStartBT + 1];
        [btn.titleLabel setFont:[UIFont fontWithName:@"Arial" size:20]];
        [btn setTitle:loc(@"返回") forState:UIControlStateNormal];
        [btn setTitleColor:btn_color forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageWithContentsOfFile:btn_bkg] forState:UIControlStateNormal];
    }
    return self;
}

#pragma mark -
#pragma mark Tableview delegate and datasource
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
	return 1;
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    
    return [arrayCook count];
}
//自定义row
-(UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier1 = @"PluPluCell1";
	RemarkViewCell *cell = (RemarkViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
	if (cell == nil){
		NSString *config = [APPSESSION.allconfig getStringValueAtPath:@"UIKit CSS/UIView CSS/plu remarks view/sub tableview/table cell style"];
		cell = (RemarkViewCell *)[[[RemarkViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier1 Customer:config] autorelease];
	}
    [cell setCelldata:[arrayCook objectAtIndex:indexPath.row]];
    return cell;
}

- (void)tableView:(UITableView *)aTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [aTableView deselectRowAtIndexPath:indexPath animated:YES];// don't keep the table selection
    
    if ([self viewWithTag:kTagOfStartText]) {
        UITextView *txtv=(UITextView *)[self viewWithTag:kTagOfStartText];
        [txtv setText:[NSString stringWithFormat:@"%@ %@",txtv.text, [arrayCook objectAtIndex:indexPath.row]]];
    }
}
//=========================================================== 
// dealloc
//=========================================================== 
- (void)dealloc
{
	[celldata release];
    celldata = nil;
    [arrayCook release];
    arrayCook=nil;
	
    [super dealloc];
}

-(void)inittextView:(NSDictionary *)cfg WithContent:(NSString *)content{
	[self setTextViewProperty:cfg WithContent:content];
}

#pragma mark 重载button处理方法
-(void) buttonClicked:(id)sender
{
    if (self.celldata == nil) {
        DebugLog(@"没有接收到celldata!");
        return;
    }
    
	NSInteger bid = [sender tag];
	if (bid == (kTagOfStartBT + 0)) {        
        DebugLog(@"点击了保存按钮");
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
		if ([self viewWithTag:kTagOfStartText]) {
			self.celldata.ddRemark = [(UITextView *)[self viewWithTag:kTagOfStartText] text];
		}
		XRmenuError *outErr=nil;
		[XRmenuCoreData updateDishOrderNum:self.celldata Increase:num(0) Error:&outErr];
		if (outErr) {
			DebugLog(@"数据保存居然失败！原因：%@", [outErr localizedDescription]);
		}
        
        //刷新tableview
        postN(kMessage_MyOrderView_ReloadTableViewData);
        //退出备注窗口
        postN(kMessage_RootView_PluNotesViewDisappear);
	}
	else if (bid == (kTagOfStartBT + 1)) {        
        DebugLog(@"点击了返回按钮");
        [APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"buttonClicked.caf"]];
		postN(kMessage_RootView_PluNotesViewDisappear);
	}
}
@end
